home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / reparameterizeNurbsCurve.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  5.5 KB  |  225 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17.  
  18. //
  19. //    Description :
  20. //        A script to reparameterize the knot sequence of a selected NURBS Curve to 
  21. //        the specified range [a,b].   
  22. //
  23. //    LIMITATIONS :
  24. //        1. The NURBS curve should have NO history.
  25. //        2. The "rescale" keeps no history.
  26. //        3. a < b.
  27. //
  28. //  How to use:  select a curve (eg. curve1) and enter these commands to 
  29. //  reparameterize the curve from 0 to 10:
  30. //
  31. //      reparameterizeNurbsCurve 0 10;  // performs the reparameterization
  32. //        getAttr curve1.minValue;        // will return 0 
  33. //        getAttr curve1.maxValue;        // will return 10
  34. //      nurbsCurveKnots;                // will print knot vector of selected curve
  35. //
  36.  
  37.  
  38. proc float[] rescale( 
  39.     float $knots[], 
  40.     float $startParm, 
  41.     float $endParm ) 
  42. //
  43. //    Description :
  44. //        To rescale the knots to lie inbetween [0,1]
  45. //
  46. {
  47.     float $scaledKnots[] ;
  48.     
  49.     int $n = size($knots) ;
  50.     if( $n == 0 ) return $scaledKnots ;
  51.  
  52.     float $minKnot = $knots[0] ;
  53.     float $maxKnot = $knots[0] ;
  54.     int $i ;
  55.  
  56.     // get min, max knot.
  57.     //
  58.     for( $i = 1 ; $i < $n ; $i++ ) {
  59.         $minKnot = `min $minKnot $knots[$i]` ;
  60.         $maxKnot = `max $minKnot $knots[$i]` ;    
  61.     }
  62.     float $newMin = $startParm ;
  63.     float $newMax = $endParm ;
  64.     float $diff = $maxKnot - $minKnot ;
  65.     float $scaleFactor = $endParm - $startParm ;
  66.     float $scale = $scaleFactor / $diff ;
  67.     for( $i = 0 ; $i < $n ; $i++ ) {
  68.         float $v = ( $knots[$i] - $minKnot ) ; 
  69.         $scaledKnots[$i] = $newMin + ($v * $scale) ; 
  70.     }
  71.     return $scaledKnots ;
  72. }
  73.  
  74. proc string buildTemporaryCurve( float $knots[], int $deg, int $ns ) 
  75. //
  76. //    Description :
  77. //        To build a temporary curve, multiple end Knots !
  78. //
  79. {
  80.     int $n = size($knots) ;
  81.     if( $n == 0 || $ns == 0  ) return " " ;
  82.  
  83.  
  84.     // append degree.
  85.     //
  86.     string $args ;
  87.     $args =  "curve" + " -d " + $deg ; 
  88.  
  89.     int $ncv = $deg + $ns ;
  90.     int $i ;
  91.  
  92.     // append dummy x, y, z values.
  93.     //
  94.     for( $i = 0 ; $i < $ncv ; $i++ ) {    
  95.         float $v = 0.0 ;
  96.         $args = $args +  " -p " + $v ;
  97.         $args = $args + " " + $v ;  
  98.         $args = $args + " " + $v ;  
  99.     }
  100.  
  101.     // append knots.
  102.     //
  103.     for( $i = 0 ; $i < $n ; $i++ ) {
  104.         $args = $args + " -k " ;
  105.         $args = $args + $knots[$i] ;
  106.     }
  107.  
  108.     $args = $args + " ; " ;
  109.  
  110.     string $crvName ;
  111.  
  112.     $crvName = eval($args) ;
  113.     return $crvName ;
  114. }
  115.  
  116. proc int rebuildCurveToMatchKnots( string $crv, string $matchCrv )
  117. //
  118. //    Description :
  119. //
  120. {
  121.     int $ok = 1 ; 
  122.     string $nodes[] ;
  123.     if( catch( $nodes = `rebuildCurve -ch false -rpo true -rt 2 -kr 1 -kcp 1 -kep 1 -kt 0 $crv $matchCrv` ) ) {
  124.         warning "rebuild to match knots failed" ;
  125.         $ok = 0 ;
  126.     }    
  127.     return $ok ;
  128. }
  129.  
  130. global proc int reparameterizeNurbsCurve(
  131.     float $startParm,
  132.     float $endParm ) 
  133. //
  134. //    Description :
  135. //        To reparametrize the knot sequence of the nurbs curve
  136. //        to be in the range [$startParm, $endParam].
  137. //    NOTE : works on a NURBS curve of the selection list.
  138. //
  139. {
  140.  
  141.     // valid parameters ?
  142.     //
  143.     if( $startParm >= $endParm ) {
  144.         error "start Parameter should be less than end Parameter" ;
  145.         return 0 ;
  146.     }
  147.  
  148.     float $knotSeq[] ;
  149.  
  150.     // 0. Grab the select list.
  151.     //
  152.     string $selList[] ;
  153.     $selList = `ls -sl` ;
  154.  
  155.     // 1. Run filter to select only the NURBS curves.
  156.     //
  157.     global int $gSelectNurbsCurvesBit ;
  158.     string $crvList[] ;
  159.     $crvList = `filterExpand -ex true -sm $gSelectNurbsCurvesBit $selList` ;    
  160.     if( size($crvList) == 0 ) {
  161.         warning "No NURBS curve selected" ;
  162.         return 1;
  163.     }
  164.  
  165.     // 2. Work on the last item if more than one NURBS curve in list.
  166.     //
  167.     int $len = size($crvList) ;
  168.     string $lastCrv = $crvList[$len-1] ;
  169.     if( $len != 1 ) {
  170.         string $w = "Returning Knots for the last NURBS curve : " + $lastCrv ;
  171.         warning $w ;
  172.     }
  173.  
  174.     // 2.0 Check if curve has history.
  175.     //
  176.     string $hist[] = `listHistory -gl true -pdo true -lf true -f false $lastCrv` ;
  177.     if( size($hist) > 0 ) {
  178.         error "curve has history, rescale is pointless" ;
  179.         return 1 ;     
  180.     }        
  181.  
  182.     // 2.1 Get the degree.
  183.     //
  184.     int $deg  ;
  185.     string $inAttr = $lastCrv + ".degree" ;
  186.     $deg = `getAttr $inAttr` ;
  187.  
  188.     int $nspans ;
  189.     $inAttr = $lastCrv + ".spans" ;
  190.     $nspans = `getAttr $inAttr` ;
  191.     
  192.     // 3. Extract the Knots.
  193.     //
  194.     select -r $lastCrv ;
  195.     $knotSeq = nurbsCurveKnots() ;
  196.  
  197.     // 4. Scale the Knot Sequence to lie in between [0,1].
  198.     //
  199.     float $sKnots[] ;
  200.     $sKnots = rescale( $knotSeq, $startParm, $endParm ) ;
  201.  
  202.     // 5. Build a temporary curve with the scaled Knots
  203.     //
  204.     int $okay = 1 ;
  205.     string $tmpCurve ;
  206.     $tmpCurve = buildTemporaryCurve( $sKnots, $deg, $nspans ) ;
  207.     if( $tmpCurve == " " ) {
  208.         $okay = 0 ;
  209.     } 
  210.  
  211.     // 6. Rebuild curve inplace to match knots.
  212.     //
  213.     if( $okay == 1 )  {
  214.         $okay = rebuildCurveToMatchKnots( $lastCrv, $tmpCurve ) ;
  215.         delete $tmpCurve ;
  216.     }
  217.  
  218.     // 7. select curve for which knots are returned.
  219.     //
  220.     select -r $lastCrv ;
  221.  
  222.     return $okay ;
  223. }
  224.  
  225.